interface Props {
	findKey: string;
	filterVal: any;
	returnKey: string;
	childrenKey: string;
	returnParentName: boolean;
}
/**
 * @description 通过id获取树节点名称
 * @param {Array} tree 树
 * findKey = 'id',过滤键名
   filterVal,//过滤值
   returnKey = 'name',节点名称字段
   childrenKey = 'children',子节点集合字段
   returnParentName = false,是否返回父节点名称
 * @return {String} 名称
 */
export const getNodeName = (
	tree: Array<any>,
	{
		findKey = 'id',
		filterVal,
		returnKey = 'name',
		childrenKey = 'children',
		returnParentName = false,
	}: Props
): string => {
	if (!tree || !tree.length) return '';

	for (const item of tree) {
		if (item[findKey] === filterVal) {
			return item[returnKey];
		}
		const childResult: string = getNodeName(item[childrenKey], {
			findKey,
			filterVal,
			returnKey,
			childrenKey,
			returnParentName,
		});
		if (childResult) {
			return returnParentName ? `${item[returnKey]}/${childResult}` : childResult;
		}
	}
	return '';
};
